package biz

import (
	"context"
	"time"

	"lianzi_gos/internal/mods/mpweixin/dal"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/errors"
	"lianzi_gos/pkg/util"
)

// 产品属性表
type GoodsAttr struct {
	Trans        *util.Trans
	GoodsAttrDAL *dal.GoodsAttr
}

// Query goods attrs from the data access object based on the provided parameters and options.
func (a *GoodsAttr) Query(ctx context.Context, params schema.GoodsAttrQueryParam) (*schema.GoodsAttrQueryResult, error) {
	params.Pagination = false

	result, err := a.GoodsAttrDAL.Query(ctx, params, schema.GoodsAttrQueryOptions{
		QueryOptions: util.QueryOptions{
			OrderFields: []util.OrderByParam{
				{Field: "created_at", Direction: util.DESC},
			},
		},
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

// Get the specified goods attr from the data access object.
func (a *GoodsAttr) Get(ctx context.Context, id string) (*schema.GoodsAttr, error) {
	goodsAttr, err := a.GoodsAttrDAL.Get(ctx, id)
	if err != nil {
		return nil, err
	} else if goodsAttr == nil {
		return nil, errors.NotFound("", "Goods attr not found")
	}
	return goodsAttr, nil
}

// Create a new goods attr in the data access object.
func (a *GoodsAttr) Create(ctx context.Context, formItem *schema.GoodsAttrForm) (*schema.GoodsAttr, error) {
	goodsAttr := &schema.GoodsAttr{
		ID:        util.NewXID(),
		CreatedAt: time.Now(),
	}

	if err := formItem.FillTo(goodsAttr); err != nil {
		return nil, err
	}
	err := a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.Create(ctx, goodsAttr); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return goodsAttr, nil
}

// Create a new goods attr in the data access object.
func (a *GoodsAttr) GoodsAttrCreate(ctx context.Context, formItem *schema.GoodsAttrForm) (*schema.GoodsAttr, error) {
	goodsAttr := &schema.GoodsAttr{
		ID:        util.NewXID(),
		CreatedAt: time.Now(),
	}
	if err := formItem.FillTo(goodsAttr); err != nil {
		return nil, err
	}

	err := a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.Create(ctx, goodsAttr); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return goodsAttr, nil
}

// Update the specified goods attr in the data access object.
func (a *GoodsAttr) Update(ctx context.Context, id string, formItem *schema.GoodsAttrForm) error {
	goodsAttr, err := a.GoodsAttrDAL.Get(ctx, id)
	if err != nil {
		return err
	} else if goodsAttr == nil {
		return errors.NotFound("", "Goods attr not found")
	}

	if err := formItem.FillTo(goodsAttr); err != nil {
		return err
	}
	goodsAttr.UpdatedAt = time.Now()
	goodsAttr.Status = "enabled"
	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.Update(ctx, goodsAttr); err != nil {
			return err
		}
		return nil
	})
}

// Update the specified goods attr in the data access object.
func (a *GoodsAttr) UpdateAttrStatus(ctx context.Context, id string, formItem *schema.GoodsAttrStatus) error {
	goodsAttr, err := a.GoodsAttrDAL.Get(ctx, id)
	if err != nil {
		return err
	} else if goodsAttr == nil {
		return errors.NotFound("", "Goods attr not found")
	}
	if err := formItem.FillToStatus(goodsAttr); err != nil {
		return err
	}
	//goodsAttr.UpdatedAt = time.Now()
	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.UpdateOneStatus(ctx, id, goodsAttr.Status); err != nil {
			return err
		}
		return nil
	})
}

// Update the specified goods attr in the data access object.
func (a *GoodsAttr) UpdateAttrPrice(ctx context.Context, id string, formItem *schema.GoodsAttrPrice) error {
	goodsAttr, err := a.GoodsAttrDAL.Get(ctx, id)
	if err != nil {
		return err
	} else if goodsAttr == nil {
		return errors.NotFound("", "Goods attr not found")
	}
	if err := formItem.FillToPrice(goodsAttr); err != nil {
		return err
	}
	//goodsAttr.UpdatedAt = time.Now()
	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.UpdatePrice(ctx, goodsAttr); err != nil {
			return err
		}
		return nil
	})
}

// Update the specified goods attr in the data access object.
func (a *GoodsAttr) UpdateStatus(ctx context.Context, id string, formItem *schema.GoodsAttrForm) error {
	goodsAttr, err := a.GoodsAttrDAL.Get(ctx, id)
	if err != nil {
		return err
	} else if goodsAttr == nil {
		return errors.NotFound("", "Goods attr not found")
	}

	if err := formItem.FillTo(goodsAttr); err != nil {
		return err
	}
	goodsAttr.UpdatedAt = time.Now()
	goodsAttr.Status = "enabled"
	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.Update(ctx, goodsAttr); err != nil {
			return err
		}
		return nil
	})
}

// Delete the specified goods attr from the data access object.
func (a *GoodsAttr) Delete(ctx context.Context, id string) error {
	exists, err := a.GoodsAttrDAL.Exists(ctx, id)
	if err != nil {
		return err
	} else if !exists {
		return errors.NotFound("", "Goods attr not found")
	}

	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.GoodsAttrDAL.Delete(ctx, id); err != nil {
			return err
		}
		return nil
	})
}
